package baseTest;

public class exist {
    public static void main(String[] args) {

    }

    public boolean process(char[][] board,String word){
        char[] words = word.toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (dfs(board,words,i,j,0)) return true;
            }
        }
        return false;
    }
    public boolean dfs(char[][] board,char[] words,int right,int down,int k){
        if (right >= board.length || right<0 || k>board[0].length || down < 0|| board[right][down] != words[k]) return false;
        if (k == words.length-1) return true;
        board[right][down] = '\0';
        boolean res = dfs(board,words,right+1,down,k+1) || dfs(board, words, right - 1, down, k + 1) ||
                dfs(board, words, right, down + 1, k + 1) || dfs(board, words, right , down - 1, k + 1);
        board[right][down] = words[k];
        return res;
    }
}
